<?php

namespace common\models;

use common\helpers\commonApi;
use common\helpers\TransformApi;
use Yii;

/**
 * This is the model class for table "{{%campus}}".
 *
 * @property int $id
 * @property string|null $name 名称
 * @property string|null $contacts 联系人
 * @property string|null $phone 电话
 * @property string|null $province 省
 * @property string|null $city 市
 * @property string|null $county 县/区
 * @property string|null $address 详细地址
 * @property string|null $longitude 经度
 * @property string|null $latitude 纬度
 * @property int|null $status 状态
 * @property string|null $create_time 创建时间
 * @property string|null $create_user 创建人
 * @property string|null $update_time 修改时间
 * @property string|null $update_user 修改人
 * @property int|null $delete 删除
 * @property float|null $can_brokerage 可提现金额
 * @property float|null $total_brokerage 总提现金额
 * @property float|null $already_brokerage 已提现金额
 * @property int|null $payment_userid 收款人
 */
class Campus extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%campus}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name','contacts','phone','province'], 'required'],
            [['status', 'delete','payment_userid'], 'integer'],
            [['create_time', 'update_time'], 'safe'],
            [['name', 'contacts', 'phone', 'province', 'city', 'county', 'address', 'longitude', 'latitude', 'create_user', 'update_user'], 'string', 'max' => 255],
            [['can_brokerage','total_brokerage','already_brokerage'], 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '名称',
            'contacts' => '联系人',
            'phone' => '电话',
            'province' => '省',
            'city' => '市',
            'county' => '县/区',
            'address' => '详细地址',
            'longitude' => '经度',
            'latitude' => '纬度',
            'status' => '状态',
            'create_time' => '创建时间',
            'create_user' => '创建人',
            'update_time' => '修改时间',
            'update_user' => '修改人',
            'delete' => '删除',
            'can_brokerage' => '可提现金额',
            'total_brokerage' => '总提现金额',
            'already_brokerage' => '已提现金额',
            'payment_userid' => '收款人',
        ];
    }

    /**
     * 获取所有校区
     * @param $where 条件
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getCampusAll($where,$select = ['*'])
    {
        return self::find()->select($select)->where($where)->asArray()->all();
    }

    /**
     * 定位校区
     * @param $lon 经度
     * @param $lat 纬度
     * @return array
     */
    public function getUser($lon,$lat)
    {
        $model = self::find()->where(['status'=>1,'delete'=>0])
            ->select(["(st_distance(point(longitude,latitude),point($lon,$lat))*111195/1000) as juli",'id','name'])
            ->orderBy(['juli'=>SORT_ASC,'create_time'=>SORT_DESC])->one();
        if (!$model){
            return commonApi::message('暂无校区可选择',false);
        }
        return commonApi::message('ok',true,['campus_id'=>$model->id,'name'=>$model->name]);
    }

    /**
     * 获取校区列表
     * @param $lon 经度
     * @param $lat 纬度
     * @return array
     */
    public function getList($lon,$lat)
    {
        $model = self::find()->where(['status'=>1,'delete'=>0])
            ->select(["(st_distance(point(longitude,latitude),point($lon,$lat))*111195/1000) as juli",'id as campus_id','name'])
            ->orderBy(['juli'=>SORT_ASC,'create_time'=>SORT_DESC])
            ->asArray()->all();
        foreach ($model as &$item){
            $juli = TransformApi::decimalPoint($item['juli'],3);
            $item['juli'] = $juli.'km';
        }
        return commonApi::message('ok',true,$model);
    }

    /**
     * 获取校区名称
     * @param $ids
     * @return array
     */
    public function getCampusName($ids)
    {
        $ids = array_unique($ids);
        $data = self::getCampusAll(['id'=>$ids],['id','name']);
        return array_column($data,'name','id');
    }
}
